package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.common.PageBean;
import com.itheima.dto.ClazzAddDto;
import com.itheima.dto.ClazzQueryDto;
import com.itheima.mapper.ClazzMapper;
import com.itheima.pojo.Clazz;
import com.itheima.service.ClazzService;
import com.itheima.vo.ClazzVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ClazzServiceImpl implements ClazzService {

    @Autowired
    ClazzMapper clazzMapper;

    @Override
    public PageBean query(ClazzQueryDto dto) {
        PageHelper.startPage(dto.getPage(),dto.getPageSize());
        Page<ClazzVo> query = (Page<ClazzVo>)clazzMapper.query(dto);

        List<ClazzVo> result = query.getResult();
        long total = query.getTotal();

        PageBean pageBean = new PageBean(total, result);
        /*PageBean pageBean = new PageBean();
        List<Clazz> selectall = clazzMapper.selectall();
        pageBean.setRows(selectall);
        pageBean.setTotal(123l);*/
        return pageBean;
    }

    @Override
    public void add(ClazzAddDto dto) {
        dto.setCreateTime(LocalDateTime.now());
        dto.setUpdateTime(LocalDateTime.now());
        clazzMapper.insert(dto);
    }

    @Override
    public void deleteid(Integer id) {
        clazzMapper.deleteid(id);
    }

    @Override
    public Clazz selectid(Integer id) {
        Clazz seleteid = clazzMapper.seleteid(id);
        return seleteid;
    }

    @Override
    public void update(Clazz clazz) {
       clazz.setUpdateTime(LocalDateTime.now());
        clazzMapper.update(clazz);
    }

    @Override
    public List<Clazz> selectall() {
        List<Clazz> selectall = clazzMapper.selectall();
        return selectall;
    }
}
